const enterpriseVue = new Vue({
  el: '#app',
  data() {
    return {
      pickerOptions: pickerOptions,
      tableData: [],
      fileList2: [{ name: 'food.jpeg', url: '../../../../admin/images/act.jpg' }],
      dialogImageUrl: '',
      dialogVisible: false,
      form: {
        Prepayment_Card_Page_SerialNumber: '',//主键
        Picture_URL: '',//图片url
        Prepayment_Card_Page_Name: '',//版面名
        Enterprise_Info_SerialNumber: $.cookie("enterprise"),//企业
      },
      loading: false
    }
  },
  mounted() {
  },
  methods: {
    handleRemove(){
      _this.form.Picture_URL = ''
    },
    Specifications(file) {
      let _this = this
      let num = 1
      if (file.size > 150000 && file.size < 500000) {
        num = 0.4
      } else if (file.size > 500000) {
        num = 0.2
      }
      console.log(file);
      new Compressor(file, {
        quality: num,
        success(file) {
          const formData = new FormData();
          formData.append('file', file, file.name);
          let oFileReader = new FileReader();
          oFileReader.readAsDataURL(file);
          oFileReader.onload = function (e) {
            console.log(e.target.result)
            $.ajax({
              type: 'POST',
              url: DomainName + '/FileList',
              data: {
                Data: e.target.result,
                DataLength: file.size,
                FileName: file.name
              },
              complete: function (XMLHttpRequest) {
                _this.loading = false
                if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                  window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                }
              },
              success: function (Result) {
                console.log(Result)
                if (Result.Status == "success") {
                  _this.form.Picture_URL = Result.Picture_Url
                  _this.$message.success('上传成功')
                } else {
                  _this.$message.error('上传失败')
                }
              },
              error: function () {
                _this.$message.error("操作失败，请稍后再试")
              },
              dataType: "json"
            });
          }
        },
        error(err) {
          console.log(err.message);
        },
      });
    },
    //查看图片
    handlePictureCardPreview(file) {
      this.dialogVisible = true
      this.dialogImageUrl = file
    },
    submitForm(formName) {
      let _this = this
      _this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(_this.form)
          _this.loading = true
          $.ajax({
            url: DomainName + '/getAddAction',
            type: 'POST',
            data: {
              Data: JSON.stringify({
                prepayment_card_page: [_this.form]
              }),
              token: $.cookie("token")
            },
            success: function (res) {
              if (res.Result == "成功") {
                _this.$message({
                  message: '提交成功',
                  type: 'success'
                });
                window.history.go(-1)
              } else {
                _this.$message.error(res.Result);
              }
            },
            error: function () {
              console.log(error)
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            }
          })
        }
        else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  }
})